package com.sgb.kjwl.view.ui.fragment.packers;

import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.bumptech.glide.Glide;
import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FrPackerSearchFindPackersBinding;
import com.sgb.kjwl.di.packers.DaggerPackersFragmentComponent;
import com.sgb.kjwl.di.packers.PackersFragmentModule;
import com.sgb.kjwl.model.entity.FindPackersSearchEntity;
import com.sgb.link.entity.PickFilterEntity;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.packers.PickResultEntity;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.packers.PackersSearchActivity;
import com.sgb.kjwl.view.ui.adapter.packers.FindPackerSearchAdapter;
import com.sgb.kjwl.view.widget.PickPopWindow1;
import com.sgb.kjwl.view.widget.ThreeLevelFilterWindow;
import com.sgb.kjwl.viewmodel.packers.FragFindPackersSearchModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

import javax.inject.Inject;

/*
 * 工队搜索分离-施工工队
 */
public class FindPackersSearchFragment extends AppLazyFragment {

    @Inject
    FragFindPackersSearchModel viewModel;
    private FrPackerSearchFindPackersBinding binding;
    private FindPackerSearchAdapter adapter;

    private String keyword = "";

    //工程分类
    private ThreeLevelFilterWindow popCategory;
    private PickResultEntity pickResultProject1;
    private PickResultEntity pickResultProject2;
    private PickResultEntity pickResultProject3;
    private String projectNo = "";
    //团队规模
    private PickPopWindow1 popTeamSize;
    private PickResultEntity pickResultTeamSize;
    //工队类型
    private PickPopWindow1 popTeamType;
    private PickResultEntity pickResultTeamType;
    //所在区域
    private ThreeLevelFilterWindow popCity;
    private PickResultEntity pickResultProvince;
    private PickResultEntity pickResultCity;
    private PickResultEntity pickResultArea;

    public static FindPackersSearchFragment newInstance() {
        FindPackersSearchFragment fragment = new FindPackersSearchFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerPackersFragmentComponent
                .builder()
                .appComponent(appComponent)
                .packersFragmentModule(new PackersFragmentModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fr_packer_search_find_packers, container, false);
        return binding.getRoot();
    }

    @Override
    protected void initView() {
        keyword = ((PackersSearchActivity) getActivity()).getEditContent();

        binding.tvProject.setOnClickListener(this);
        binding.tvTeamSize.setOnClickListener(this);
        binding.tvTeamType.setOnClickListener(this);
        binding.tvArea.setOnClickListener(this);

        if (adapter == null) {
            adapter = new FindPackerSearchAdapter(getActivity(), null, Glide.with(this));
            binding.recyclerView.setAdapter(adapter);
        }
        adapter.setOnItemClick(new AdapterOnItemClick<FindPackersSearchEntity>() {
            @Override
            public void onItemClick(FindPackersSearchEntity entity, int position) {
                viewModel.getH5Url(entity.getNo());
            }
        });

        binding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.recyclerView.setLoadingMoreEnabled(true);
        binding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(
                0,
                0,
                0,
                getActivity().getResources().getDimension(R.dimen.dp10),
                getActivity().getResources().getColor(R.color.color_F5F5F5)));
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                initFilterData(false);
            }

            @Override
            public void onLoadMore() {
                initFilterData(true);
            }
        });

        pickResultProject1 = new PickResultEntity();
        pickResultProject2 = new PickResultEntity();
        pickResultProject3 = new PickResultEntity();
        pickResultTeamSize = new PickResultEntity();
        pickResultTeamType = new PickResultEntity();
        pickResultProvince = new PickResultEntity();
        pickResultCity = new PickResultEntity();
        pickResultArea = new PickResultEntity();
    }

    private void initFilterData(boolean isLoadMore) {
        viewModel.getFindPackersSearch(keyword, projectNo, pickResultTeamSize.getName(), pickResultTeamType.getNo(),
                pickResultProvince.getNo(), pickResultCity.getNo(),
                pickResultArea.getNo(), isLoadMore);
    }

    @Override
    protected void onLazyLoad() {
        initFilterData(false);
    }

    public void refreshKeywordRequest() {
        keyword = ((PackersSearchActivity) getActivity()).getEditContent();
        initFilterData(false);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_project:
                if (popCategory == null) {
                    viewModel.getCategoryType();
                } else {
                    showCategoryPop(null);
                }
                break;
            case R.id.tv_team_size:
                if (popTeamSize == null) {
                    viewModel.getTeamSize();
                } else {
                    showTeamSizePopWindow(null);
                }
                break;
            case R.id.tv_team_type:
                if (popTeamType == null) {
                    viewModel.getTeamType();
                } else {
                    showTeamTypePopWindow(null);
                }
                break;
            case R.id.tv_area:
                if (popCity == null) {
                    viewModel.getCityList();
                } else {
                    showCityPop(null);
                }
                break;
        }
    }

    public void showFindPackerList(List<FindPackersSearchEntity> list, boolean isLoadMore, boolean isLast) {
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list != null && list.size() > 0) {
                adapter.addDatas(list);
            }
            binding.recyclerView.setNoMore(isLast);
        } else {
            binding.recyclerView.refreshComplete();
            if (list != null && list.size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                adapter.setDatas(list);
            } else {
                adapter.clearDatas();
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    //工程-工程分类
    public void showCategoryPop(List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>> data) {
        if (popCategory == null) {
            popCategory = new ThreeLevelFilterWindow(getActivity());
            popCategory.setBgView(binding.grayLayout);
            popCategory.setOnResultClick(new ThreeLevelFilterWindow.OnResultClick() {
                @Override
                public void result(Object key1, Object key2, Object key3) {
                    pickResultProject1.setNo(((PickResultEntity) key1).getNo());
                    pickResultProject1.setName(((PickResultEntity) key1).getName());
                    pickResultProject2.setNo(((PickResultEntity) key2).getNo());
                    pickResultProject2.setName(((PickResultEntity) key2).getName());
                    pickResultProject3.setNo(((PickResultEntity) key3).getNo());
                    pickResultProject3.setName(((PickResultEntity) key3).getName());
                    if (TextUtils.equals("全部", pickResultProject1.getName())) {
                        binding.tvProject.setText("工程分类");
                    } else {
                        if (TextUtils.equals("全部", pickResultProject2.getName())) {
                            binding.tvProject.setText(pickResultProject1.getName());
                            projectNo = pickResultProject1.getNo();
                        } else {
                            if (TextUtils.equals("全部", pickResultProject3.getName())) {
                                binding.tvProject.setText(pickResultProject2.getName());
                                projectNo = pickResultProject2.getNo();
                            } else {
                                binding.tvProject.setText(pickResultProject3.getName());
                                projectNo = pickResultProject3.getNo();
                            }
                        }
                    }
                    initFilterData(false);
                }

                @Override
                public void reset() {
                    pickResultProject1.clear();
                    pickResultProject2.clear();
                    pickResultProject3.clear();
                    projectNo = "";
                    binding.tvProject.setText("工程分类");
                    initFilterData(false);
                }
            });
            popCategory.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    if (TextUtils.isEmpty(pickResultProject1.getNo())) {
                        setTextViewChange(binding.tvProject, R.mipmap.ic_down_arrow_n, R.color.color_666666);
                    } else {
                        setTextViewChange(binding.tvProject, R.mipmap.ic_down_arrow, R.color.new_blue);
                    }
                }
            });

            popCategory.setThreeLevelData(data);
        }
        setTextViewChange(binding.tvProject, R.mipmap.ic_up_arrow, R.color.new_blue);
        popCategory.showPopwindow0(binding.clLayout);
    }

    //团队规模pop
    public void showTeamSizePopWindow(List<PickSelectEntity> data) {
        if (popTeamSize == null) {
            popTeamSize = new PickPopWindow1(getContext());
            popTeamSize.setBgView(binding.grayLayout);
            popTeamSize.setOnResultClick(new PickPopWindow1.OnResultClick() {
                @Override
                public void result(Object key1, Object key2, Object key3) {
                    pickResultTeamSize.setNo((String) key1);
                    pickResultTeamSize.setName((String) key2);
                    pickResultTeamSize.setPosition((int) key3);
                    if (TextUtils.isEmpty((String) key2) || TextUtils.equals("全部", (String) key2)) {
                        binding.tvTeamSize.setText("团队规模");
                    } else {
                        binding.tvTeamSize.setText((String) key2);
                    }
                    initFilterData(false);
                }
            });
            popTeamSize.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    if (TextUtils.isEmpty(pickResultTeamSize.getNo()) || TextUtils.equals("全部", pickResultTeamSize.getName())) {
                        setTextViewChange(binding.tvTeamSize, R.mipmap.ic_down_arrow_n, R.color.color_666666);
                    } else {
                        setTextViewChange(binding.tvTeamSize, R.mipmap.ic_down_arrow, R.color.new_blue);
                    }
                }
            });

            popTeamSize.setData(data);
        }
        setTextViewChange(binding.tvTeamSize, R.mipmap.ic_up_arrow, R.color.new_blue);
        popTeamSize.showPopwindow0(binding.clLayout);
    }

    //工队类型pop
    public void showTeamTypePopWindow(List<PickSelectEntity> data) {
        if (popTeamType == null) {
            popTeamType = new PickPopWindow1(getContext());
            popTeamType.setBgView(binding.grayLayout);
            popTeamType.setOnResultClick(new PickPopWindow1.OnResultClick() {
                @Override
                public void result(Object key1, Object key2, Object key3) {
                    pickResultTeamType.setNo((String) key1);
                    pickResultTeamType.setName((String) key2);
                    pickResultTeamType.setPosition((int) key3);
                    if (TextUtils.isEmpty((String) key2) || TextUtils.equals("全部", (String) key2)) {
                        binding.tvTeamType.setText("工队类型");
                    } else {
                        binding.tvTeamType.setText((String) key2);
                    }
                    initFilterData(false);
                }
            });
            popTeamType.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    if (TextUtils.isEmpty(pickResultTeamType.getNo()) || TextUtils.equals("全部", pickResultTeamType.getName())) {
                        setTextViewChange(binding.tvTeamType, R.mipmap.ic_down_arrow_n, R.color.color_666666);
                    } else {
                        setTextViewChange(binding.tvTeamType, R.mipmap.ic_down_arrow, R.color.new_blue);
                    }
                }
            });

            popTeamType.setData(data);
        }
        setTextViewChange(binding.tvTeamType, R.mipmap.ic_up_arrow, R.color.new_blue);
        popTeamType.showPopwindow0(binding.clLayout);
    }

    //省市区pop
    public void showCityPop(List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>> data) {
        if (popCity == null) {
            popCity = new ThreeLevelFilterWindow(getActivity());
            popCity.setBgView(binding.grayLayout);
            popCity.setOnResultClick(new ThreeLevelFilterWindow.OnResultClick() {
                @Override
                public void result(Object key1, Object key2, Object key3) {
                    pickResultProvince.setNo(((PickResultEntity) key1).getNo());
                    pickResultProvince.setName(((PickResultEntity) key1).getName());
                    pickResultCity.setNo(((PickResultEntity) key2).getNo());
                    pickResultCity.setName(((PickResultEntity) key2).getName());
                    pickResultArea.setNo(((PickResultEntity) key3).getNo());
                    pickResultArea.setName(((PickResultEntity) key3).getName());
                    if (TextUtils.equals("全部", pickResultProvince.getName())) {
                        binding.tvArea.setText("所在地区");
                    } else {
                        if (TextUtils.equals("全部", pickResultCity.getName())) {
                            binding.tvArea.setText(pickResultProvince.getName());
                        } else {
                            if (TextUtils.equals("全部", pickResultArea.getName())) {
                                binding.tvArea.setText(pickResultCity.getName());
                            } else {
                                binding.tvArea.setText(pickResultArea.getName());
                            }
                        }
                    }
                    initFilterData(false);
                }

                @Override
                public void reset() {
                    pickResultProvince.clear();
                    pickResultCity.clear();
                    pickResultArea.clear();
                    binding.tvArea.setText("所在地区");
                    initFilterData(false);
                }
            });
            popCity.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    if (TextUtils.isEmpty(pickResultProvince.getNo())) {
                        setTextViewChange(binding.tvArea, R.mipmap.ic_down_arrow_n, R.color.color_666666);
                    } else {
                        setTextViewChange(binding.tvArea, R.mipmap.ic_down_arrow, R.color.new_blue);
                    }
                }
            });

            popCity.setThreeLevelData(data);
        }
        setTextViewChange(binding.tvArea, R.mipmap.ic_up_arrow, R.color.new_blue);
        popCity.showPopwindow0(binding.clLayout);
    }

    private void setTextViewChange(TextView textView, int img, int textColor) {
        Resources res = getResources();
        Drawable drawable = res.getDrawable(img);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        textView.setCompoundDrawables(null, null, drawable, null);
        textView.setTextColor(getResources().getColor(textColor));
    }

    public void getUrl(String url) {
        WebViewX5HostActivity.start(getActivity(), url);
    }
}
